<?php
namespace app\index\controller;
use think\Controller;

/**
 * 公用基础控制器
 */
class BaseController extends Controller {

	protected $member;

	protected function _initialize() {
		parent::_initialize();
		$this->request->filter('htmlspecialchars,trim_all');
		$user=db("member")->where("id",2)->find();
		$this->assign("openid",$user['open_id']);
		$this->assign("user_info",$user);
		$this->assign("user_id",$user['id']);
		$openid=$user['open_id'];
		session("user_info",$user);
		session("user_id",$user['id']);
		$openid=$user['open_id'];
		session("openid",$openid);
		if(!session('bc2')){
		    session("user_info",null);
		    session("user_id",null);
		    session("openid",null);
			session("bc2",1);
		}
		$openid=session("openid");
		$redirect='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->add_fx($openid);
		$cut_action = request()->action();
		$this->assign('action',$cut_action);
		$cut_controller = request()->controller();
		$this->assign('controller',$cut_controller);
		if($cut_controller=='admin'||$cut_controller=='Admin'){
			return true;
		}
		if($cut_action=="wechat_notify"||$cut_action=="verify_code"||$cut_action=="shenghe"){
			return true;
		}
		$user_info=session("user_info");
		if(!$openid){
			$auth_code=input("code");
			$url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_userinfo&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
			if(!$auth_code){
				Header("Location: $url");
				exit;
			}
			$data=user_wx_id($auth_code);
			$openid=$data['openid'];
			session("openid",$openid);
			session('wx_user',$data);
			$user_info = db('member') -> where("open_id", $openid) -> find();
			if($user_info) {
				session('user_info', $user_info);
				session('user_id', $user_info['id']);
			}
			//$this -> redirect($redirect);
		}else{
			if($user_info&&!$user_info['logo']){
				$data['nick_name']=session("wx_user")['nick_name'];
				$data['logo']=session("wx_user")['headimgurl'];
				$user_id=db('member')->where("open_id", $openid)->update($data);
				$user_info = db('member') -> where("open_id", $openid) -> find();
				session('user_info', $user_info);

			}
		}
			
			$from=strpos($redirect, '?from=');
			$from1=strpos($redirect, '&from=');
			$code=strpos($redirect, '?code');
			$code1=strpos($redirect, '&code');
			if($from){
				$redirect=substr($redirect,0,$from);
				$this -> redirect($redirect);
			}elseif($from1){
				$redirect=substr($redirect,0,$from1);
				$this -> redirect($redirect);
			}elseif($code){
				$redirect=substr($redirect,0,$code);
				$this -> redirect($redirect);
			}elseif($code1){
				$redirect=substr($redirect,0,$code1);
				$this -> redirect($redirect);
			}
	}
	function add_fx($openid){
		if($openid){
			if(input("fx_id")){
				session('fx_id',input("fx_id"));
				$data['vuser_id']=input('fx_id');
			}
			$data['is_user']=0;
			$data['open_id']=$openid;
			if(session('user_id')){
				$data['is_user']=1;
				$data['nick_name']=session('user_info')['nick_name']?session('user')['nick_name']:'匿名';
				$data['logo']=session('user_info')['logo']?session('user_info')['logo']:"/public/index/images/moren.jpg";	
			}
			$data['nick_name']=session('wx_user')['nick_name'];
			$data['logo']=session('wx_user')['headimgurl'];
			$data['add_time']=now_time();
			$t_vis=db('visit')->wheretime('add_time','today')->where('open_id',$openid)->find();
			if(!$t_vis){
				db("visit")->insert($data);
			}
			
		}
	}

	/**
	 * 注册会员
	 */
	function add_user($openid){
		$appid=config('wxpay_appid');
		$secret=config('wxpay_appsercet');
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";	
		$res=file_get_contents($url);
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		$access_token=$array1['access_token'];
		$res=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN");
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		if(isset($array1['nickname'])){
			$data['nick_name']=$array1['nickname'];
			$data['logo']=$array1['headimgurl'];
			$data['is_atten']=1;
		}
		if(input("fromuid")){
			$data['pid1']=input("fromuid");
		}	
		$data['open_id']			= $openid;
		$data['add_time']		= now_time();
		$user_id=db('member')->insert($data);
		$user_info = db('member') -> where("open_id", $openid) -> find();
		session('user_info', $user_info);
	}
	public function upload1(){
		$files = $_POST["value"];  
	    $tmp = base64_decode($files);  
	    $fn = time()."_".rand(100000, 999999) . ".jpg";
		$fp = './uploads/'. $fn;
		$fp1 = '/uploads/'. $fn;
	    $res=file_put_contents( $fp, $tmp);
		if($res){
			return json(["code"=>200,'url'=>$fp1]);
		}
		return json(["code"=>404,'msg'=>'上传错误']);
		
	}
}

